<template>
  <Dialog :title="dialogTitle" v-model="dialogVisibleForm" width="700px" class="voice-dialog">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="120px"
        v-loading="formLoading"
        class="voice-form"
      >
        <!-- 基本信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:info-filled" class="section-icon" />
            <span>基本信息</span>
          </div>

          <el-form-item label="文件名称" prop="name" class="form-item">
            <el-input
              class="form-input"
              v-model.trim="formData.name"
              placeholder="请输入语音文件名称"
              clearable
            >
              <template #prefix>
                <Icon icon="ep:document" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="上传类型" prop="type" class="form-item">
            <el-select
              class="form-select"
              v-model="formData.type"
              placeholder="请选择上传类型"
              clearable
            >
              <el-option label="手动" :value="1">
                <div class="option-item">
                  <div class="option-content">
                    <span class="option-label">手动上传</span>
                    <span class="option-desc">上传本地音频文件</span>
                  </div>
                </div>
              </el-option>
              <el-option label="语音合成" :value="2">
                <div class="option-item">
                  <div class="option-content">
                    <span class="option-label">语音合成</span>
                    <span class="option-desc">通过TTS技术合成语音</span>
                  </div>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </div>

        <!-- TTS配置区域 -->
        <div class="form-section" v-if="formData.type == 2">
          <div class="section-title">
            <Icon icon="ep:microphone" class="section-icon" />
            <span>TTS配置</span>
          </div>

          <el-form-item label="TTS厂商" class="form-item">
            <el-select
              class="form-select"
              v-model="formData.tts"
              placeholder="请选择TTS厂商"
              clearable
            >
              <el-option label="科大讯飞" :value="2">
                <div class="option-item">
                  <Icon icon="ep:microphone" class="option-icon" />
                  <div class="option-content">
                    <span class="option-label">科大讯飞</span>
                    <span class="option-desc">专业的语音合成服务</span>
                  </div>
                </div>
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="音色" prop="voiceType" class="form-item">
            <el-select
              class="form-select"
              v-model="formData.voiceType"
              placeholder="请选择音色"
              clearable
            >
              <el-option
                :label="item.timbreName"
                v-for="item in timbreList"
                :key="item.id"
                :value="item.timbreId"
              >
                <div class="option-item">
                  <Icon icon="ep:user" class="option-icon" />
                  <span class="option-label">{{ item.timbreName }}</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="合成文本" prop="content" class="form-item">
            <el-input
              type="textarea"
              v-model.trim="formData.content"
              placeholder="请输入要合成的文本内容\n支持多行文本，系统将自动转换为语音"
              :rows="4"
              class="form-textarea"
              show-word-limit
              maxlength="500"
            ></el-input>
            <div class="input-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>文本长度建议控制在500字以内，以获得更好的合成效果</span>
            </div>
          </el-form-item>
        </div>

        <!-- 文件上传区域 -->
        <div class="form-section" v-if="formData.type == 1">
          <div class="section-title">
            <Icon icon="ep:upload" class="section-icon" />
            <span>文件上传</span>
          </div>

          <el-form-item label="语音文件" prop="filePath" class="form-item">
            <div class="upload-container">
              <UploadFile
                v-model="formData.filePath"
                :file-type="['mp3', 'wav']"
                :limit="1"
                :file-size="10"
                class="upload-component"
              />
              <div class="upload-tip">
                <Icon icon="ep:warning" class="tip-icon" />
                <div class="tip-content">
                  <p>支持格式：MP3、WAV</p>
                  <p>文件大小：不超过10MB</p>
                  <p>建议：采样率16kHz，比特率128kbps</p>
                </div>
              </div>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button class="cancel-btn" @click="dialogVisibleForm = false">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          type="primary"
          class="submit-btn"
          :disabled="formLoading"
          :loading="formLoading"
          @click="submitForm"
        >
          <Icon icon="ep:check" class="btn-icon" v-if="!formLoading" />
          {{ formLoading ? '提交中...' : '确定' }}
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import { Close } from '@element-plus/icons-vue'
import * as VoiceApi from '@/api/call/voiceManagement'

const dialogTitle = ref('操作')
const dialogVisibleForm = ref(false)
const { t } = useI18n()

const emit = defineEmits(['success'])

const formLoading = ref(false)
const formRules = reactive({
  name: [
    {
      required: true,
      message: '名单名称不能为空',
      trigger: 'blur'
    }
  ],
  type: [
    {
      required: true,
      message: '请选择上传类型',
      trigger: 'change'
    }
  ],
  tts: [
    {
      required: true,
      message: '请选择tts厂商',
      trigger: 'change'
    }
  ],
  filePath: [
    {
      required: true,
      message: '请上传语音文件',
      trigger: 'change'
    }
  ],
  content: [
    {
      required: true,
      message: '请输入合成文本',
      trigger: 'blur'
    }
  ],
  voiceType: [
    {
      required: true,
      message: '请选择音色',
      trigger: 'change'
    }
  ]
})
const formData = reactive({
  id: '',
  name: null,
  type: '',
  tts: '',
  content: '',
  filePath: '',
  voiceType: ''
})
const timbreList = ref([])
const formType = ref(null)
const open = async (type, idValue) => {
  dialogVisibleForm.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  getTimbreList()
  if (type == 'update') {
    const { id, name, type, tts, content, filePath, voiceType } = await VoiceApi.getVoice(idValue)
    Object.assign(formData, { id, name, type, tts, content, filePath, voiceType })
  } else {
    formData.name = null
    formData.id = ''
    formData.type = ''
    formData.tts = ''
    formData.content = ''
    formData.filePath = ''
    formData.voiceType = ''
  }
}
defineExpose({ open })
const formRef = ref(null)
const getTimbreList = async () => {
  timbreList.value = await VoiceApi.getTimbre()
}
/*提交*/
const submitForm = async () => {
  const valid = await formRef.value.validate()
  if (!valid) return
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      await VoiceApi.createIvrVoice(formData)
      ElMessage.success(t('common.createSuccess'))
    } else {
      await VoiceApi.updateIvrVoice(formData)
      ElMessage.success(t('common.updateSuccess'))
    }
    dialogVisibleForm.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
// 对话框样式
.voice-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;

    .el-dialog__header {
      background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
      color: white;
      padding: 20px 24px;

      .el-dialog__title {
        color: white;
        font-size: 18px;
        font-weight: 600;
      }

      .el-dialog__headerbtn {
        .el-dialog__close {
          color: rgba(255, 255, 255, 0.8);
          font-size: 18px;

          &:hover {
            color: white;
          }
        }
      }
    }

    .el-dialog__body {
      padding: 0;
    }

    .el-dialog__footer {
      padding: 0;
      border-top: 1px solid #f1f5f9;
    }
  }
}

// 表单容器
.form-container {
  padding: 24px;
  max-height: 70vh;
  overflow-y: auto;
}

.voice-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

// 区域标题
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f1f5f9;

  .section-icon {
    color: #667eea;
    font-size: 18px;
  }

  span {
    font-size: 16px;
    font-weight: 600;
    color: #374151;
  }
}

// 表单项
.form-item {
  margin-bottom: 24px;

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    line-height: 1.6;
  }

  :deep(.el-form-item__content) {
    line-height: normal;
  }
}

// 输入框样式
.form-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

// 选择框样式
.form-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 选项样式
.option-item {
  display: flex;
  align-items: center;
  gap: 12px;

  .option-icon {
    font-size: 16px;
    color: #6b7280;
    flex-shrink: 0;
  }

  .option-content {
    display: flex;
    flex-direction: column;
    gap: 2px;

    .option-label {
      font-weight: 500;
      color: #374151;
    }

    .option-desc {
      font-size: 12px;
      color: #6b7280;
    }
  }

  .option-label {
    font-weight: 500;
    color: #374151;
  }
}

// 文本域样式
.form-textarea {
  :deep(.el-textarea__inner) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;
    font-family: inherit;
    resize: vertical;

    &:hover {
      border-color: #d1d5db;
    }

    &:focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 输入提示
.input-tip {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 8px;
  padding: 12px;
  background: #f0f9ff;
  border: 1px solid #e0f2fe;
  border-radius: 8px;

  .tip-icon {
    color: #0ea5e9;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  span {
    font-size: 13px;
    color: #0369a1;
    line-height: 1.4;
  }
}

// 上传容器
.upload-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.upload-component {
  :deep(.el-upload) {
    width: 100%;
  }
}

.upload-tip {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 16px;
  background: #fef3c7;
  border: 1px solid #fbbf24;
  border-radius: 8px;

  .tip-icon {
    color: #f59e0b;
    font-size: 18px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .tip-content {
    p {
      margin: 0 0 4px 0;
      font-size: 13px;
      color: #92400e;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

// 对话框底部
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #f8fafc;
}

.cancel-btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid #e5e7eb;
  background: white;
  color: #6b7280;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }
}

.submit-btn {
  padding: 10px 24px;
  border-radius: 8px;
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border: none;
  color: white;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .voice-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;

      .el-dialog__header {
        padding: 16px 20px;

        .el-dialog__title {
          font-size: 16px;
        }
      }
    }
  }

  .form-container {
    padding: 20px;
    max-height: 60vh;
  }

  .voice-form {
    :deep(.el-form-item__label) {
      font-size: 14px;
    }
  }

  .section-title {
    span {
      font-size: 15px;
    }
  }

  .option-content {
    .option-desc {
      display: none;
    }
  }

  .upload-tip {
    .tip-content {
      p {
        font-size: 12px;
      }
    }
  }

  .dialog-footer {
    padding: 16px 20px;

    .cancel-btn,
    .submit-btn {
      padding: 12px 16px;
      font-size: 14px;
    }
  }
}

@media (max-width: 480px) {
  .voice-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      height: 100vh;
      border-radius: 0;
    }
  }

  .form-container {
    padding: 16px;
    max-height: calc(100vh - 140px);
  }

  .form-section {
    margin-bottom: 24px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .upload-container {
    gap: 12px;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;

    .cancel-btn,
    .submit-btn {
      width: 100%;
      justify-content: center;
    }
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .voice-dialog {
    :deep(.el-dialog) {
      background: #1e293b;

      .el-dialog__body {
        background: #1e293b;
      }
    }
  }

  .form-container {
    background: #1e293b;
  }

  .section-title {
    border-color: #334155;

    span {
      color: #f1f5f9;
    }
  }

  .form-item {
    :deep(.el-form-item__label) {
      color: #e2e8f0;
    }
  }

  .option-content {
    .option-label {
      color: #f1f5f9;
    }

    .option-desc {
      color: #94a3b8;
    }
  }

  .input-tip {
    background: #1e3a8a;
    border-color: #1e40af;

    span {
      color: #93c5fd;
    }
  }

  .upload-tip {
    background: #451a03;
    border-color: #92400e;

    .tip-content p {
      color: #fdba74;
    }
  }

  .dialog-footer {
    background: #0f172a;
    border-color: #334155;
  }
}
</style>
